import puppeteer from "puppeteer";

export async function generatePDF(url, outputPath = "report.pdf") {
  const browser = await puppeteer.launch({
    executablePath: 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
    headless: true,
    args: ["--no-sandbox", "--disable-setuid-sandbox"],
  });

  const page = await browser.newPage();
  await page.goto(url, { waitUntil: "networkidle0" });

  await page.pdf({
    path: outputPath,
    format: "A4",
    printBackground: true,
    displayHeaderFooter: true,
    headerTemplate: `
      <div style="width:100%; font-size:10px; text-align:center; padding:5px; border-bottom:1px solid #ccc;">
        ACME 公司销售报告 - 2025
      </div>`,
    footerTemplate: `
      <div style="width:100%; font-size:10px; text-align:center; padding:5px; border-top:1px solid #ccc;">
        第 <span class="pageNumber"></span> / <span class="totalPages"></span> 页
      </div>`,
    margin: { top: "70px", bottom: "60px", left: "20px", right: "20px" },
  });

  await browser.close();
  console.log("✅ PDF 已生成：" + outputPath);
}
